package com.cdu.controller;

import com.cdu.commons.MallConstants;
import com.cdu.commons.R;
import com.cdu.pojo.dto.ProductQueryDTO;
import com.cdu.pojo.entity.User;
import com.cdu.pojo.vo.PageVO;
import com.cdu.pojo.vo.ProductVO;
import com.cdu.service.ProductService;
import com.cdu.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @ProjectName: cdu-app
 * @Titile: ProductController
 * @Author: Administrator
 * @Description: 商品控制类
 */
@RestController
@RequestMapping("/api/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 分页查询商品列表
     */
    @GetMapping("list")
    public R<PageVO<ProductVO>> getProductByPage(ProductQueryDTO productQueryDTO, HttpServletRequest request){
        //从请求中获取身份
        String token = request.getHeader(MallConstants.TOKEN_SIGN);
        User user = null;
        if(StringUtils.hasLength(token)){
            //解析token
            user = JwtUtils.parseToken(token);
        }
        PageVO<ProductVO> productList = productService.getProductListByPage(productQueryDTO, user);
        return R.ok("商品查询成功",productList);
    }
}
